# gozerlib/persist/pdol.py
#
#

""" persisted dict of lists. persist to database or memcache. """

from persist import Persist
from mempersist import MPersist
 
class Pdol(Persist):

    """ persisted dict of lists to datasbase backend. """

    def __init__(self, fname):
        Persist.__init__(self, fname)

        if not self.data:
            self.data = {}

    def __iter__(self, name):

        """ 
             iter over list. 

             :param name: name of list to iter
             :type name: string
             :rtype: iterator

        """

        return self.data[name].__iter__()
 
    def __getitem__(self, item):

        """
            get item.

            :param item: item to get
            :type item: string
            :rtype: list

        """

        if self.data.has_key(item):
            return self.data[item]

    def __delitem__(self, item):

        """ 
            delete item.

            :param item: name of item to delete
            :type item: string
            :rtype: boolean

        """

        if self.data.has_key(item):
            self.data.__delitem__(item)
            return True

        return False

    def __setitem__(self, item, what):

        """
            set item.

            :param item: name of item to set
            :type item: string 
            :param what: value to set
            :type what: dict, list, string, integer or None
            :rtype: boolean

        """

        self.data[item] = what
        return True

    def add(self, item, what):

        """
            add what to items list. 

            :param item: item to set
            :type item: string
            :param what: value to set
            :type what: dict, list, string, integer or None
            :rtype: ?
            
        """

        return self.__setitem__(item, what)

    def adduniq(self, item, what):

        """
            add what to items list if item not yet added.

            :param item: item to set
            :type item: string
            :param what: value to set
            :type what: dict, list, string, integer or None
            :rtype: ?

        """

        if not self.data.has_key(item):
            self.new(item)

        if what not in self.data[item]:
            return self.__setitem__(item, what)

    def get(self, item):

        """
            get items list.

            :param item: item to get
            :type item: string
            :rtype: dict, list, string, integer or None

        """

        return self.__getitem__(item)

    def new(self, what):
 
        """
            reset list of what.
 
            :param what: key of list to reset
            :type what: string
            :rtype: None

        """

        self.data[what] = []

    def delete(self, item, what):

        """
            remove what from item's list.

            :param item: key of list to delete item from
            :type item: string
            :param what: index into list
            :type what: integer
            :rtype: None

        """

        del self.data[item][what]

    def extend(self, item, what):

        """
            extend list. 

            :param item: key of list to delete item from
            :type item: string
            :param what: index into list
            :type what: integer
            :rtype: None

        """

        if not self.data.has_key(item):
            self.new(item)

        self.data[item].extend(what)

    def remove(self, item, what):

        """
            remove what. 

            :param item: key of list to delete item from
            :type item: string
            :param what: index into list
            :type what: dict, list, string, integer or None
            :rtype: boolean

        """

        try:
            self.data[item].remove(what)
            return True
        except (ValueError, KeyError):
            return False

class MPdol(Pdol, MPersist):

    """ perstist dict of lists to memcache. """

    pass
